<?php
error_reporting(E_ALL);

class instagram{
	public $userid = '28312815';
	public $accessToken = '28312815.ab103e5.8281b1261dee4106a8ca3b79b5fe6feb';
	
	private function fetchdata($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		$result = curl_exec($ch);
		curl_close($ch); 
		return $result;
	}//fetchdata
	
	public function getrecent($maxid){
		if($maxid <= 0){
			$result = $this->fetchData('https://api.instagram.com/v1/users/'.$this->userid.'/media/recent/?access_token='.$this->accessToken);
			$result = json_decode($result);
			//print_r($result);
			$this->showimages($result);
		}//if
		else{
			$result = $this->fetchData('https://api.instagram.com/v1/users/'.$this->userid.'/media/recent/?access_token='.$this->accessToken.'&max_id='.$maxid);
			$result = json_decode($result);
			$this->showimages($result);
		}//else
	}//getrecent
	
	private function showimages($result){
		foreach ($result->data as $post){
			echo "\t";
   		echo '<a class="fancybox" data-fancybox-group="gallery" href="'.$post->images->standard_resolution->url.'" title="';
			$caption = $post->caption->text;
			if(strlen($caption) > 70)
				echo substr($caption, 0, 70).'...';
			else
				echo $caption;
			echo '"><img src="'.$post->images->thumbnail->url.'"></a> ';
			echo "\n";
		}//foreach
		echo "\t";
		echo '<div id="load'.$result->pagination->next_max_id.'" class="load_more">';
		echo "\n\t\t";
		echo '<br><a href="#" class="load_more btn" id="'.$result->pagination->next_max_id.'">Load more</a>';
		echo "\n\t";
		echo '</div>';
	}//showimages
}//instagram
?>